<?php
namespace app\house\model;
use app\BaseModel;
use think\facade\Db;



class AdminHoseStaff extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_house_staff';
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }


    /**
     * @param $dis
     * @功能说明:推荐顾问列表
     * @author chenniang
     * @DataTime: 2020-02-19 13:05
     */
    public function staffList($dis){
        $data = $this->where($dis)->status(-1)->order('sort desc')->select()->toArray();
        return $data;
    }

    /**
     * @param $data
     * @功能说明:推荐顾问添加
     * @author chenniang
     * @DataTime: 2020-02-19 13:06
     */
    public function staffAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }

    /**
     * User: chenniang
     * Date: 2019-09-27 19:03
     * @param $dis
     * @return bool
     * descption:添加顾问删除
     */
    public function staffDel($dis){
        $res = $this->where($dis)->delete();
        return $res;
    }

    /**
     * User: chenniang
     * Date: 2019-09-27 19:03
     * @param $dis
     * @return bool
     * descption:添加顾问查询
     */
    public function staffInfo($dis){
        $res = $this->where($dis)->find();
        return !empty($res)?$res:[];
    }


    /**
     * @param $dis
     * @功能说明:查询员工id
     * @author chenniang
     * @DataTime: 2020-02-19 13:22
     */
    public function staffColumn($dis){

        return $this->where($dis)->column('staff_id');

    }


    /**
     * @param $dis
     * @功能说明:查询员工姓名
     * @author chenniang
     * @DataTime: 2020-02-20 11:20
     */
    public function getStaff($dis){
        $data = $this->alias('a')
            ->join('longbing_card_user_info b','b.fans_id = a.staff_id')
            ->where($dis)
            ->field('a.staff_id as id,b.name')
            ->group('a.id')
//            ->order('a.id desc')
            ->select()
            ->toArray();
        if(!empty($data)){
            foreach ($data as &$v){
                $v['name']   = !empty($v['name'])?$v['name']:Db::name('longbing_card_user')->where(['id'=>$v['id']])->value('nickName');
            }
        }
        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 15:55
     * @功能说明:获取员工
     */
    public function getStaffLimit($dis,$Limit=3){

        $data = $this->alias('a')
                ->join('longbing_card_user_info b','b.fans_id = a.staff_id AND b.is_staff=1')
                ->join('longbing_card_job c','c.id = b.job_id','left')
                ->where($dis)
                ->field('a.staff_id,b.name as user_name,b.avatar as user_avatar,c.name as job_name')
                ->group('a.id')
                ->order('a.staff_id desc')
                ->limit($Limit)
                ->select()
                ->toArray();
        if(!empty($data)){
            foreach ($data as &$v){
                $v['user_name']   = !empty($v['user_name'])?$v['user_name']:Db::name('longbing_card_user')->where(['id'=>$v['staff_id']])->value('nickName');
                $v['user_avatar'] = !empty($v['user_avatar'])?$v['user_avatar']:Db::name('longbing_card_user')->where(['id'=>$v['staff_id']])->value('avatarUrl');
                $v['job_name']    = !empty($v['job_name'])?$v['job_name']:'暂无职位';
                $v['user_avatar'] = !empty($v['user_avatar'])?$v['user_avatar']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
            }
        }
        $data = transImagesOne($data,['user_avatar']);

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 15:55
     * @功能说明:获取员工
     */
    public function getStaffOne($staff_id){

        $data = Db::name('longbing_card_user_info')->alias('b')
            ->join('longbing_card_job c','c.id = b.job_id','left')
            ->where(['b.fans_id'=>$staff_id,'b.is_staff'=>1])
            ->field('b.fans_id as staff_id,b.name as user_name,b.avatar as user_avatar,c.name as job_name')
            ->find();

        if(!empty($data)){

            $data['user_name']   = !empty($data['user_name'])?$data['user_name']:Db::name('longbing_card_user')->where(['id'=>$data['staff_id']])->value('nickName');
            $data['user_avatar'] = !empty($data['user_avatar'])?$data['user_avatar']:Db::name('longbing_card_user')->where(['id'=>$data['staff_id']])->value('avatarUrl');
            $data['job_name']    = !empty($data['job_name'])?$data['job_name']:'暂无职位';
            $data['user_avatar'] = !empty($data['user_avatar'])?$data['user_avatar']:'https://lbqnyv2.migugu.com/defaultAvatar.png';

        }
        $data = transImagesOne($data,['user_avatar']);

        return $data;
    }


    /**
     * @author chenniang
     * @DataTime: 2020-02-19 15:55
     * @功能说明:获取员工
     */
    public function getStaffList($dis){
        $data = $this->alias('a')
            ->join('longbing_card_user_info b','b.fans_id = a.staff_id AND b.is_staff=1')
            ->join('longbing_card_job c','c.id = b.job_id','left')
            ->where($dis)
            ->field('a.staff_id,b.name as user_name,c.name as job_name,b.avatar as user_avatar')
            ->group('a.staff_id')
            ->order('a.staff_id desc')
            ->paginate(10)
            ->toArray();
        if(!empty($data['data'])){
            foreach ($data['data'] as &$v){
                $v['user_name']   = !empty($v['user_name'])?$v['user_name']:Db::name('longbing_card_user')->where(['id'=>$v['staff_id']])->value('nickName');
                $v['user_avatar'] = !empty($v['user_avatar'])?$v['user_avatar']:Db::name('longbing_card_user')->where(['id'=>$v['staff_id']])->value('avatarUrl');
                $v['job_name']    = !empty($v['job_name'])?$v['job_name']:'暂无职位';
                $v['user_avatar'] = !empty($v['user_avatar'])?$v['user_avatar']:'https://lbqnyv2.migugu.com/defaultAvatar.png';
            }
        }
        $data['data'] = transImagesOne($data['data'],['user_avatar']);
        return $data;
    }


}